package vue;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;

import vue.onglets.ListeOnglets;

public class EcranAccueil extends JPanel implements MouseListener,ActionListener
{
	private static final long serialVersionUID = 1L;
	private JDialog fen;
	private ListeOnglets listeOnglets;
	private BarreMenu barreMenu;
	private BarreOutils barreOutils;
	private Box boxH1,boxH2,boxH,boxV;
	private JLabel intitule;
	private JList jlactions;
	private JButton bOk,bAnnuler;
	private String stNouveau2D = "Créer un nouveau fichier 2D";
	private String stNouveau3D = "Créer un nouveau fichier 3D";
	private String stOuvrir = "Ouvrir un fichier existant (.spx)";
	
	public EcranAccueil(ListeOnglets listeOnglets,BarreMenu barreMenu,BarreOutils barreOutils)
	{		
		this.listeOnglets = listeOnglets;
		this.barreMenu = barreMenu;
		this.barreOutils = barreOutils;
		
		intitule = new JLabel("Que voulez-vous faire ?");
		
		DefaultListModel dlm = new DefaultListModel();
		dlm.addElement(stNouveau2D);
		dlm.addElement(stNouveau3D);
		dlm.addElement(stOuvrir);
		
		jlactions = new JList(dlm);
		jlactions.setSelectedIndex(0);
		jlactions.addMouseListener(this);
		
		bOk = new JButton("OK");
		bAnnuler = new JButton("Annuler");
		
		bOk.setMaximumSize(bAnnuler.getMinimumSize());
		
		bOk.addActionListener(this);
		bAnnuler.addActionListener(this);
		
		boxH1 = Box.createHorizontalBox();
		boxH1.add(intitule);
		
		boxH2 = Box.createHorizontalBox();
		boxH2.add(bOk);
		boxH2.add(Box.createHorizontalStrut(10));
		boxH2.add(bAnnuler);
		
		boxV = Box.createVerticalBox();
		boxV.add(Box.createVerticalStrut(15));
		boxV.add(boxH1);
		boxV.add(Box.createVerticalStrut(15));
		boxV.add(new JScrollPane(jlactions));
		boxV.add(Box.createVerticalStrut(15));
		boxV.add(boxH2);
		boxV.add(Box.createVerticalStrut(15));
		
		boxH = Box.createHorizontalBox();
		boxH.add(Box.createHorizontalStrut(15));
		boxH.add(boxV);
		boxH.add(Box.createHorizontalStrut(15));
		
		this.setLayout(new BorderLayout());
		this.add(boxH,BorderLayout.CENTER);
		this.setPreferredSize(new Dimension(300,200));
		
		fen = new JDialog();
		fen.add(this);
		fen.setTitle("Bienvenue sur Simplexe 1.0 !");
		fen.getRootPane().setDefaultButton(bOk);
		fen.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		fen.setResizable(false);
		fen.setLocation(320,200);
		fen.setModal(true);
		fen.pack();
	}
	
	public JDialog getJDialog()
	{
		return fen;
	}

	public void mouseClicked(MouseEvent e){}

	public void mousePressed(MouseEvent e) 
	{
		if(e.getClickCount() == 2)
		{
			this.traitement();
		}
	}

	public void mouseReleased(MouseEvent e){}

	public void mouseEntered(MouseEvent e){}

	public void mouseExited(MouseEvent e){}

	public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource() == bOk)
		{
			this.traitement();
		}
		
		if(e.getSource() == bAnnuler)
		{
			fen.dispose();
		}
	}	
	
	private void traitement()
	{		
		String action = (String)jlactions.getSelectedValue();
		if(action != null)
		{
			fen.dispose();
			if(action.equals(stNouveau2D))
			{
				listeOnglets.ajoutOnglet("Nouveau2D");
				barreMenu.agirItemsOnglet();
				barreOutils.agirItemsBouton();
			}
			if(action.equals(stNouveau3D))
			{
				listeOnglets.ajoutOnglet("Nouveau3D");	
				barreMenu.agirItemsOnglet();
				barreOutils.agirItemsBouton();
			}
			if(action.equals(stOuvrir))
			{
				listeOnglets.ouvrir();
				barreMenu.agirItemsOnglet();
				barreOutils.agirItemsBouton();
			}
		}
	}
}